<?php

namespace app\controller;

use app\BaseController;
use app\service\AuthService;
use app\validate\AuthValidate;
use think\App;
use think\facade\Cache;

class AuthController extends BaseController
{
    private $service;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->service = new AuthService();
    }

    public function AdminLogin(){
        $data = $this->request->param(['username' , 'password']);
        validate(AuthValidate::class)
            ->check($data);
        $result = $this->service->LoginCheck($data['username'] , $data['password'] , 'admin');
        return $this->returnJson($result);
    }

    public function testApi(){
        Cache::delete('getInformation');
        return $this->returnJson(json_decode(Cache::get('getInformation')));
    }

    public function StudentLogin(){
        $data = $this->request->param(['username' , 'password']);
        validate(AuthValidate::class)
            ->check($data);
        $result = $this->service->LoginCheck($data['username'] , $data['password'] , 'student');
        return $this->returnJson($result);
    }

    public function StudentRegister(){
        $data = $this->request->param(['username' , 'password']);
        validate(AuthValidate::class)
            ->check($data);
        $result = $this->service->Register($data['username'] , $data['password']);
        return $this->returnJson($result);
    }
}